package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.JWTUtils;
import com.huixuebao.exchange.dto.DataDTO;
import com.huixuebao.exchange.dto.LoginDTO;
import com.huixuebao.exchange.dto.SmsCodeDTO;
import com.huixuebao.exchange.entity.CaptchaEntity;
import com.huixuebao.exchange.entity.UserEntity;
import com.huixuebao.exchange.mapper.CaptchaMapper;
import com.huixuebao.exchange.mapper.UserMapper;
import com.huixuebao.exchange.service.CaptchaService;
import com.huixuebao.exchange.service.LoginService;
import com.huixuebao.exchange.util.CaptchaUtil;
import com.huixuebao.exchange.util.SendSms;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    CaptchaMapper captchaMapper;

    @Autowired
    CaptchaService captchaService;
    @Override
    public ApiResult login(LoginDTO loginDTO) {
        ApiResult apiResult = new ApiResult();
        try {
            String phone = loginDTO.getPhone();
            String captchaValue = loginDTO.getCaptchaValue();
            UserEntity userPhone = userMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, phone));
            String mobileNo = userPhone.getPhone();
            if (captchaService.checkCaptcha(null, CaptchaUtil.CAPTCHA_TYPE_MOBILE, captchaValue, mobileNo)) {
                if (StrUtil.isNotEmpty(userPhone.getCid()) && StrUtil.isNotEmpty(mobileNo)) {
                    DataDTO dataDTO = new DataDTO();
                    Map<String, String> hashMap = new HashMap<>();
                    dataDTO.setUserId(userPhone.getId());
                    //hashMap.put("userId", userPhone.getId());
                    //String accessToken = JWTUtils.getToken(hashMap);
                    //dataDTO.setData(accessToken);
                    apiResult.setData(dataDTO);
                    apiResult.setSuccess(true);
                    apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                    apiResult.setMessage("手机验证码登陆成功!!");
                    captchaMapper.delete(new LambdaQueryWrapper<CaptchaEntity>().eq(CaptchaEntity::getPhone, mobileNo));
                } else {
                    apiResult.setSuccess(false);
                    apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                    apiResult.setMessage("手机号未注册用户!!");
                }
            } else {
                apiResult.setSuccess(false);
                apiResult.setMessage("请输入正确到验证码信息!!");
            }
        } catch (Exception e) {
            log.info("login:" + e.getMessage());
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("登陆异常!!" + e.getMessage());
        }
        return apiResult;
    }

    @Override
    public ApiResult getSmsCode(SmsCodeDTO smsCodeDTO) {
        ApiResult apiResult = new ApiResult();
        try {
            String userPhone = smsCodeDTO.getUserPhone();
            UserEntity userCode = userMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, userPhone));
            String phone = userCode.getPhone();
            if (userCode != null) {
                String smsCode = CaptchaUtil.generateCode();
                SendSms.sendCaptchaCode(phone, smsCode);
                CaptchaEntity captcha = new CaptchaEntity();
                captcha.setCaptchaType(CaptchaUtil.CAPTCHA_TYPE_MOBILE);
                captcha.setCaptchaValue(smsCode);
                captcha.setCreateTime(new Date());
                captcha.setPhone(phone);
                captchaMapper.insert(captcha);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("汇学保理赔平台发送验证码成功!!");
            } else {
                log.info("当前手机号为理赔平台未查询到当前手机号");
                apiResult.setSuccess(false);
                apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
                apiResult.setMessage("当前未查询到你的账号，请联系客服!!");
            }
        } catch (Exception e) {
            log.info("发送验证码失败!!");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("发送短信验证码失败!!");
        }
        return apiResult;
    }
}
